using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Localization;

namespace Sinbiota.Web.Extensions
{
    public static class HtmlHelperExtensions
    {
        public static string YesOrNo(this HtmlHelper html, bool test)
        {
            return test ? Strings.Yes : Strings.No;
        }

        public static string ImageActionLink(this HtmlHelper html, int id, string action, string text, string imageurl)
        {
            return html.ActionLink("REPLACE", action, new { id }).ToString().Replace("REPLACE", "<img alt='" + text + "'title='" + text + "' border=0 src='" + imageurl + "' >");
        }
        public static string ImageActionLink(this HtmlHelper html, int id, string controller, string action, string text, string imageurl)
        {
            return html.ActionLink("REPLACE", action, controller, new { id }, null).ToString().Replace("REPLACE", "<img alt='" + text + "'title='" + text + "' border=0 src='" + imageurl + "' >");
        }

        public static string RemoveAccents(this HtmlHelper html, string txt)
        {
            var s = txt.Normalize(NormalizationForm.FormD);

            var sb = new StringBuilder();

            foreach (var t in
                from t in s
                let uc = CharUnicodeInfo.GetUnicodeCategory(t)
                where uc != UnicodeCategory.NonSpacingMark
                select t)
            {
                sb.Append(t);
            }
            return sb.ToString();
        }

        public static MvcHtmlString EnumDropDownList<T>(this HtmlHelper helper, object htmlAttributes = null)
        {
            return EnumDropDownList<T>(helper, typeof(T).Name, htmlAttributes);
        }

        public static MvcHtmlString EnumDropDownList<T>(this HtmlHelper helper, string name, object htmlAttributes = null)
        {
            var type = typeof(T);
            if (!type.IsEnum) throw new ArgumentException("Type is not an enum.");
            var enums = (from int value in Enum.GetValues(type)
                         select new SelectListItem
                         {
                             Value = value.ToString(),
                             Text = Strings.ResourceManager.GetString(Enum.GetName(type, value)) ?? Enum.GetName(type, value)
                         }).ToList();
            return helper.DropDownList(name, enums, htmlAttributes);
        }

    }
}